const app = getApp();
// import trigger from '../../utils/util.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    curIndex: 0,
    curkm: "10000",
    curMinute: 30,
    nav_on_3: 'on',
    isShow: false,
    isRun: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    app.updateFlag(this)
    app.updateRun()
    // this.getLocation()
    let that = this
    console.log(wx.getStorageSync('tipsNum'), wx.getStorageSync('date'))
    // app.checkSession(this, '../play/index', '', '', '', '', '', true, 'true');
    // trigger.trigger(this, '../play/index')
    // this.getData()
  },
  getData() {
    let that = this
    wx.showLoading({
      title: '查询订单中...',
    })
    app.request(app.apiDomain + 'index.php?c=step&a=getStep', {
      openid: wx.getStorageSync('openid'),
      type: 2
    }, function (res) {
      wx.hideLoading()
      console.log(res)
      if (res.data.status == 0) {
        that.setData({
          hasOrder: 0
        })
      } else if (res.data.status == 1) {
        let mile = parseInt(res.data.data.mileage)
        that.setData({
          hasOrder: 1,
          order_id: res.data.data.id,
          curIndex: mile == 10000 ? '0' : mile == 15000 ? '1' : mile == 20000 ? '2' : mile == 30000 ? '3' : mile == 40000 ? '4' : '5',
          curkm: parseInt(res.data.data.mileage),
          curMinute: mile == 10000 ? '2' : mile == 15000 ? '4' : mile == 20000 ? '6' : mile == 30000 ? '8' : mile == 40000 ? '10' : '5',
        })
      } else {
        that.setData({
          hasOrder: 2,
          info: res.data.info
        })
      }
    })
  },
  // 获取定位授权
  getLocation: function () {
    let that = this;
    //选择位置，需要用户授权
    wx.getSetting({
      // success(res) {
      //   if (!res.authSetting['scope.userLocation']) {
      //     wx.authorize({
      //       scope: 'scope.userLocation',
      //       success() {
      //         wx.showToast({ //这里提示失败原因
      //           title: '授权成功！',
      //           duration: 1500
      //         })
      //       },
      //       fail() {
      //         wx.showToast({
      //           title: '您还没有授权位置信息，可能不能正常使用该功能',
      //           icon: 'none'
      //         });
      //       }
      //     })
      //   }
      // }
    })
  },
  toggleGold: function (e) {
    var that = this
    console.log(e)
    if (that.data.hasOrder == 0 || that.data.hasOrder == 2) {
      let curIndex = e.target.dataset.id
      let curkm = e.target.dataset.km
      let curMinute = e.target.dataset.minute
      that.setData({
        curIndex,
        curkm,
        curMinute
      })
    } else if (that.data.hasOrder == 1) {
      wx.showToast({
        title: '当天运动挑战还未结束，请完成挑战吧.',
        icon: 'none'
      })
      return false
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    app.SystemInfo(this)
  },
  closeTip() {
    let that = this
    let hasOpen = true
    if (!wx.getStorageSync('tipsNum')) {
      wx.setStorageSync('tipsNum', 1)
      hasOpen = false
    } else if (parseInt(wx.getStorageSync('tipsNum')) == 1) {
      wx.setStorageSync('tipsNum', 2)
      hasOpen = false
    } else {
      wx.setStorageSync('tipsNum', 3)
      hasOpen = false
    }
    that.setData({
      tipsNum: parseInt(wx.getStorageSync('tipsNum')),
      hasOpen: hasOpen
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this
    // app.checkSession(this, '../play/index', '', '', '', '', '', true, 'true');
    let hasOpen = true
    if (!wx.getStorageSync('tipsNum')) {
      // wx.setStorageSync('tipsNum', 1)
      let date = new Date().getDate()
      let month = new Date().getMonth() + 1
      let year = new Date().getFullYear()
      wx.setStorageSync('date', year + '-' + month + '-' + date)
    } else if (parseInt(wx.getStorageSync('tipsNum')) == 1) {
      let str = wx.getStorageSync('date')
      let arr = str.split('-')
      let date = parseInt(new Date().getDate())
      let month = parseInt(new Date().getMonth()) + 1
      let year = parseInt(new Date().getFullYear())
      console.log('时间', year == parseInt(arr[0]) && month == arr[1], year == parseInt(arr[0]) && month > arr[1], year > parseInt(arr[0]) && month == arr[1], year > parseInt(arr[0]) && month > arr[1], date > parseInt(arr[2]))
      if (year == parseInt(arr[0]) && month == arr[1]) {
        if (date > parseInt(arr[2])) {
          // wx.setStorageSync('tipsNum', 2)
          hasOpen = true
        } else {
          // wx.setStorageSync('tipsNum', 1)
          hasOpen = false
        }
      }
      if (year == parseInt(arr[0]) && month > arr[1] || year > parseInt(arr[0]) && month == arr[1] || year > parseInt(arr[0]) && month > arr[1]) {
        // wx.setStorageSync('tipsNum', 2)
        hasOpen = true
      }

    } else {
      // wx.setStorageSync('tipsNum', 3)
      hasOpen = false
    }
    that.setData({
      tipsNum: parseInt(wx.getStorageSync('tipsNum')),
      hasOpen: hasOpen
    })
    that.setData({
      curIndex: 0,
      curkm: "10000",
      curMinute: 2,
    })
    if (!app.isAuth) {

    } else if (!app.isRun) {

    } else {
      app.request(app.apiDomain + 'index.php?c=step&a=getStep', {
        openid: wx.getStorageSync('openid'),
        type: 2
      }, function (res) {
        console.log(res)
        if (res.data.status == 0) {
          that.setData({
            hasOrder: 0
          })
        } else if (res.data.status == 1) {
          let mile = parseInt(res.data.data.mileage)
          that.setData({
            hasOrder: 1,
            order_id: res.data.data.id,
            curIndex: mile == 10000 ? '0' : mile == 15000 ? '1' : mile == 20000 ? '2' : mile == 30000 ? '3' : mile == 40000 ? '4' : '5',
            curkm: parseInt(res.data.data.mileage),
            curMinute: res.data.data.minute,
          })
        } else {
          that.setData({
            hasOrder: 2,
            info: res.data.info
          })
        }
      })
    }
  },
  authSuccess(e) {
    this.setData({
      isShow: false
    })
  },
  authFail(e) {
    this.setData({
      isShow: false
    })
  },
  runSuccess() {
    this.setData({
      isRun: false
    })
    this.getData()
  },
  runFail() {
    this.setData({
      isRun: false
    })
  },
  click() {
    this.setData({
      isShow: false
    })
  },
  goto: function () {
    let that = this
    if (!app.isAuth) {
      that.setData({
        isShow: true
      })
    } else {
      if (!app.isRun) {
        that.setData({
          isRun: true
        })
      } else {
        let km = parseFloat(that.data.curkm).toString()
        let timestamp = new Date().getTime()
        let year = new Date().getFullYear()
        let month = new Date().getMonth() + 1
        let date = new Date().getDate()
        let timestamp_last = new Date(year + '/' + month + '/' + date + ' 23:30:00').getTime()
        console.log(timestamp, timestamp_last)
        if (that.data.hasOrder == 0) {
          wx.navigateTo({
            url: './run?km=' + km + '&time=' + that.data.curMinute + '&status=0',
          })
        } else if (that.data.hasOrder == 1) {
          wx.navigateTo({
            url: './run?km=' + km + '&time=' + that.data.curMinute + '&status=1&order_id=' + that.data.order_id,
          })
        } else {
          wx.showToast({
            title: that.data.info,
            icon: 'none',
            duration: 2000
          })
        }
      }
    }
  },
  history_click: function () {
    wx.navigateTo({
      url: './record',
    })
  },
  return_act() {
    wx.reLaunch({
      url: '../activityList/index',
    })
  },
  // 授权
  // getUserInfo: function () {
  //   var that = this
  //   if (app.getUserInfo(that)) {
  //     that.setData({
  //       user_auth: false
  //     })
  //   }
  // },
  run_auth: function (openid, e) {
    wx.openSetting({

    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var nickName = '';
    var userId = 0;
    try {
      nickName = wx.getStorageSync('nickname');
      userId = wx.getStorageSync('user_id');
    } catch (e) { }
    return {
      path: "/pages/play/index?user_id=" + userId,
      title: '运动挑战，让您的每一步充满激情。',
      imageUrl: app.resourceDomain + 'canvas/healthy/img/play/play_tz.png'
    }
  }
})